(function(){
	var AjaxCrud = QualityCheck.AjaxCRUD;
	var serverUrl = QualityCheck.GPUD.ServiceConfig;
	var currentPage = 1;
	var pageSize = 10;
	var totalSize;
	var now = 0;
	var dataCache = [];
	$(function(){
		var usercode = $("#user-name",parent.parent.document).attr("code");
		var userkey = $("#user-name",parent.parent.document).attr("key");
		var usertype = $("#user-name",parent.parent.document).attr("usertype");
		keyCheck(usercode,usertype,userkey,function(){
			initSmPages();
			window.onresize = function () {
				initSmPages();
			};
			pagebtns();
			initClass();
		});
	});
	function initClass(){
		var userKey = $("#user-name",parent.parent.document).attr("key");
		var schoolcode = $("#user-name",parent.parent.document).attr("schoolcode");
		var data = {
				cmd:"getGradeAndClass",
				model:"tclass",
				userKey:userKey,
				data:{
					schoolcode:schoolcode
				},
				remark:"",						
		};
		AjaxCrud.get2(serverUrl.ServerLoginUrl,data,function(res){
			if(res.result == 1 && res.data != undefined && res.data != ""){
				var data = res.data; 
				studentSeachMenuCaches = data;
				var contents = "";
				for(var i = 0; i < data.length; i++){
					if(i == 0){
						contents += "<option selected='selected' classcodes='"+data[i].classcodes+"' value='"+data[i].classes+"'>"+data[i].grade+"</option>";
						var classes = data[i].classes;
						var classcodes = data[i].classcodes;
						$("#student-class").empty();
						var classArr = classes.split(",");
						var classcodeArr = classcodes.split(",");
						var cont = "";
						for(var j = 0; j < classArr.length; j++){
							if(j == 0){
								cont += "<option selected='selected' value='"+classcodeArr[j]+"'>"+classArr[j]+"</option>";
							}else{
								cont += "<option value='"+classcodeArr[j]+"'>"+classArr[j]+"</option>";
							}
						}
						$("#student-class").append(cont);						
					}else{
						contents += "<option classcodes='"+data[i].classcodes+"' value='"+data[i].classes+"'>"+data[i].grade+"</option>";
					}
					
				}
				$("#student-grade").empty();
				$("#student-grade").append(contents);
				$("#student-grade").unbind("change");
				$("#student-grade").change(function(){
					var classes = $("#student-grade option:selected").val();
					var classcodes = $("#student-grade option:selected").attr("classcodes");
					$("#student-class").empty();
					var classArr = classes.split(",");
					var classcodeArr = classcodes.split(",");
					var cont = "";
					for(var i = 0; i < classArr.length; i++){
						cont += "<option value='"+classcodeArr[i]+"'>"+classArr[i]+"</option>";
					}
					$("#student-class").append(cont);						
				});
				initCourse();
			}else{
				
			}
		},null,null);
	}
	function initCourse(){
		var userKey = $("#user-name",parent.parent.document).attr("key");	
		var classcode = $("#student-class option:selected").val();
		var className =$("#student-grade option:selected").text() + "年级" + $("#student-class option:selected").text() + "班";
		$("#course-table-title").text(className + "课程表");
		var data = {
				cmd:"getCourseByStudentCode",
				model:"course",
				userKey:userKey,
				data:{
					classcode:classcode
				},
				remark:""
		};
		AjaxCrud.get(serverUrl.ServerLoginUrl,data,function(res){
			var content = "<tr style='font-weight: 700;background-color:#00ABF0;color:#fff;'>" +
								"<td width='50px'>序号</td>" +
								"<td width='100px'>时间安排</td>" +
								"<td width='100px'>星期一</td>" +
								"<td width='100px'>星期二</td>" +
								"<td width='100px'>星期三</td>" +
								"<td width='100px'>星期四</td>" +
								"<td width='100px'>星期五</td>" +
							"</tr>";
			for(var j = 0; j < 8; j++){
				content += "<tr>" + 
						"<td style='background-color:#FBB829;color:#fff;' width='50px'>" + (j+1) + "</td>" +
						"<td style='color:#6F6F6F' class='table-course-date' courseno='" + (j+1) + "' width='121px'>" + "" + "</td>" +
						"<td style='color:#6F6F6F' class='table-course-point' coursecode='' subjectcodeno='' courseno='" + (j+1) + "' weekno='1' width='100px'>" + "" + "</td>" +
						"<td style='color:#6F6F6F' class='table-course-point' coursecode='' subjectcodeno='' courseno='" + (j+1) + "' weekno='2' width='100px'>" + "" + "</td>" +
						"<td style='color:#6F6F6F' class='table-course-point' coursecode='' subjectcodeno='' courseno='" + (j+1) + "' weekno='3' width='100px'>" + "" + "</td>" +
						"<td style='color:#6F6F6F' class='table-course-point' coursecode='' subjectcodeno='' courseno='" + (j+1) + "' weekno='4' width='100px'>" + "" + "</td>" +
						"<td style='color:#6F6F6F' class='table-course-point' coursecode='' subjectcodeno='' courseno='" + (j+1) + "' weekno='5' width='100px'>" + "" + "</td>" +
					"</tr>";
			}
			$(".course-table-body").empty();
			$(".course-table-body").append(content);
			if(res.result == 1 && res.data != undefined && res.data != ""){
				var data = res.data;
				var list = data.course;
				for(var i = 0; i < list.length; i++){
					for(var key in list[i]){
						if(list[i][key] == null || list[i][key] == "null"){
							list[i][key] = "";
						}
					}
					var courseIndex = parseInt(list[i]["courseno"]*7) + parseInt(list[i]["weekno"]) + 1;
					var timeIndex = parseInt(list[i]["courseno"]*7) + 1;
					var subjectcodeno = list[i]["subjectcodeno"];
					var coursecode = list[i]["coursecode"];
					$(".course-table-body").find("td")[courseIndex].innerHTML = list[i]["subjectcode"];
					$(".course-table-body").find("td")[courseIndex].setAttribute("subjectcodeno",subjectcodeno);
					$(".course-table-body").find("td")[courseIndex].setAttribute("coursecode",coursecode);
					$(".course-table-body").find("td")[timeIndex].innerHTML = list[i]["coursetime"];
				}
			}
			$(".table-course-date").unbind("click");
			$(".table-course-date").click(function(e){
				var me = $(this);
				var thisIndex = me.parent().index();
				var dateArr = getDefaultTime(thisIndex).split("-");
				var conts = "<div class='time-input-se'><input value='"+dateArr[0]+"' type='text' id='start-hour' class='Wdate' readonly='readonly' onclick=\"WdatePicker({dateFmt: 'HH:mm'})\"> - <input type='text' value='"+dateArr[1]+"' id='end-hour' class='Wdate' readonly='readonly' onclick=\"WdatePicker({dateFmt: 'HH:mm'})\"></div><div id='sure-time-btn'>确定</div>";
				var dialogP = $(".index-dialog").position();
				var left = e.pageX - dialogP.left;
				var top = e.pageY - dialogP.top;
				$(".course-plan-dialog").width(240);
				$(".course-plan-dialog").empty();
				$(".course-plan-dialog").append(conts);
				$(".course-plan-dialog").show();
				$(".course-plan-dialog").css("left",left + "px");
				$(".course-plan-dialog").css("top",top + "px");
				$("#sure-time-btn").click(function(){
					var time = $("#start-hour").val() + "-" + $("#end-hour").val();
					me.text(time);
					$(".course-plan-dialog").hide();
				});
			});
			$(".table-course-point").unbind("click");
			$(".table-course-point").click(function(e){
				var coursePoint = $(this);
				getAllCourseSubject(function(res){
					var allClass = res;
					if(allClass.length > 0){
						var conts = "<div class='course-plan-dialog-div' subjectcode = ''>清空</div>";
						for(var i = 0; i < allClass.length; i++){
							conts += "<div class='course-plan-dialog-div' subjectcode = '" + allClass[i].subjectcode + "'>" + allClass[i].coursecontent + "</div>";
						}
						var dialogP = $(".index-dialog").position();
						var left = e.pageX - dialogP.left;
						var top = e.pageY - dialogP.top;
						$(".course-plan-dialog").width(300);
						$(".course-plan-dialog").empty();
						$(".course-plan-dialog").append(conts);
						$(".course-plan-dialog").show();
						$(".course-plan-dialog").css("left",left + "px");
						$(".course-plan-dialog").css("top",top + "px");
						$(".course-plan-dialog div").click(function(){
							if($(this).text() == "清空"){
								coursePoint.text("");
							}else{
								coursePoint.text($(this).text());
							}
							coursePoint.attr("subjectcodeno",$(this).attr("subjectcode"));
							$(".course-plan-dialog").hide();
						});
					}else{
						
					}
				});
			});
			
		},null,null);
	}
	function addCourseTable(list){
		processBegin("正在修改中···");
		var userKey = $("#user-name",parent.parent.document).attr("key");	
		var data = {
				cmd:"addOrUpdate",
				model:"course",
				userKey:userKey,
				data:list,
				remark:""
		};
		AjaxCrud.add(serverUrl.ServerLoginUrl,data,function(res){
			if(res.result == 1 && res.data != undefined && res.data != ""){
				processEnd("安排课程表成功");
				closeDialog(function(){});
			}else{
				alertBox("unsuccess","安排课程表失败",false);
			}
		},null,null);
	}
	function getAllCourseSubject(callback){
		var list = [];
		var userKey = $("#user-name",parent.parent.document).attr("key");	
		var data = {
				cmd:"getAllCourseSubject",
				model:"course",
				userKey:userKey,
				data:{
					type:1
				},
				remark:"",
				
		};
		AjaxCrud.get(serverUrl.ServerLoginUrl,data,function(res){
			callback(res.data);
		},null,null);
		return list;
	}
	function getDefaultTime(a){
		switch (a){
			case 1:
				return "08:00-08:45";
				break;
			case 2:
				return "08:55-09:40";
				break;
			case 3:
				return "10:00-10:45";
				break;
			case 4:
				return "10:55-11:40";
				break;
			case 5:
				return "13:30-14:15";
				break;
			case 6:
				return "14:25-15:10";
				break;
			case 7:
				return "15:30-16:15";
				break;
			case 8:
				return "16:25-17:10";
				break;
		}
	}
	function pagebtns(){
		$("#course-search-btn").click(function(){
			initCourse();
		});
		$("#edit-course-btn").click(function(){
			var coursetds = $(".table-course-point");
			var timetds = $(".table-course-date");
			var classcode = $("#student-class option:selected").val();
			var list = [];
			for(var i = 0; i < coursetds.length; i++){
				var subObj = {};
				for(var j = 0; j < timetds.length; j++){
					if(timetds[j].getAttribute("courseno") == coursetds[i].getAttribute("courseno")){
						subObj.coursetime = timetds[j].innerHTML;
					}
				}
				subObj.courseno = coursetds[i].getAttribute("courseno");
				subObj.weekno = coursetds[i].getAttribute("weekno");
				subObj.subjectcode = coursetds[i].getAttribute("subjectcodeno");
				subObj.coursecode = coursetds[i].getAttribute("coursecode");
				subObj.classcode = classcode;
				if(subObj.coursecode == ""){
					subObj.coursecode = classcode + subObj.subjectcode;
				}
				subObj.semester = 1;
				if(subObj.subjectcode != ""){
					list.push(subObj);
				}
			}
			addCourseTable(JSON.stringify(list));
		});
	}
	function initSmPages(){
		var rcH = $(".body-right",parent.document).height() - $(".right-title").height()-3;
		$(".right-content").height(rcH);
		
		var cdsH = $(".right-content").height() - $(".content-title").height() - 40;
		$(".content-data-show").height(cdsH);
	}
})();